package com.cqupt.scanning.system.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.handler.exception.MyException;
import com.cqupt.scanning.system.acl.entity.AclRole;
import com.cqupt.scanning.system.acl.entity.AclRoleGroup;
import com.cqupt.scanning.system.acl.entity.dto.RoleGroupDTO;
import com.cqupt.scanning.system.acl.mapper.AclRoleGroupMapper;
import com.cqupt.scanning.system.acl.service.AclRoleGroupService;
import com.cqupt.scanning.system.acl.service.AclRoleService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-19
 */
@Service
public class AclRoleGroupServiceImpl extends ServiceImpl<AclRoleGroupMapper, AclRoleGroup> implements AclRoleGroupService {

    @Autowired
    private AclRoleService aclRoleService;

    @Override
    public R addRoleGroup(RoleGroupDTO roleGroupDTO) {
        AclRoleGroup aclRoleGroup = new AclRoleGroup();
        aclRoleGroup.setRoleGroupName(roleGroupDTO.getRoleGroupName().trim());
        aclRoleGroup.setRemark(roleGroupDTO.getRemark());
        int id = baseMapper.insert(aclRoleGroup);
        if (id <= 0) return R.error().message("Add Failed");
        return R.ok().data("id", aclRoleGroup.getId());
    }

    @Override
    public R getAllRoleGroup() {
        List<AclRoleGroup> aclRoleGroups = baseMapper.selectList(null);
        List<RoleGroupDTO> roleGroupDTOS = aclRoleGroups.stream().map(aclRoleGroup -> {
            RoleGroupDTO roleGroupDTO = new RoleGroupDTO();
            BeanUtils.copyProperties(aclRoleGroup, roleGroupDTO);
            return roleGroupDTO;
        }).collect(Collectors.toList());
        return R.ok().data("roleGroups", roleGroupDTOS);
    }

    @Override
    public R updateRoleGroup(RoleGroupDTO roleGroupDTO) {
        AclRoleGroup roleGroup = new AclRoleGroup();
        BeanUtils.copyProperties(roleGroupDTO, roleGroup);
        int id = baseMapper.updateById(roleGroup);
        if (id <= 0) return R.error().message("Update failed");
        return R.ok();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public R removeRoleGroup(Integer roleGroupId) {
        try {
            aclRoleService.remove(new QueryWrapper<AclRole>().eq("role_group_id", roleGroupId));
            baseMapper.deleteById(roleGroupId);
            return R.ok();
        } catch (Exception e) {
            e.getStackTrace();
            throw new MyException(20001, "Fail to delete");
        }

    }

    @Override
    public R getGroupAndRole() {
        List<AclRoleGroup> groupAndRole = baseMapper.getGroupAndRole();
        return R.ok().data("groupAndRole", groupAndRole);
    }
}
